package com.yex.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.base.WalletOper;
import com.yex.base.WalletOperEnum;
import com.yex.base.ZeroResultException;
import com.yex.common.utils.PageUtils;
import com.yex.dao.DynamicLogDao;
import com.yex.entity.DynamicLogEntity;
import com.yex.entity.UserEntity;
import com.yex.entity.WalletEntity;
import com.yex.service.DynamicLogService;
import com.yex.service.LocaleMessageSourceService;
import com.yex.service.UserService;
import com.yex.service.WalletService;
import com.yex.utils.JsonResultVo;
import com.yex.utils.MathUtil;
import com.yex.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("dynamicLogService")
public class DynamicLogServiceImpl extends ServiceImpl<DynamicLogDao, DynamicLogEntity> implements DynamicLogService {
    @Autowired
    private UserService userService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private LocaleMessageSourceService message;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<DynamicLogEntity> page = this.selectPage(
                new Query<DynamicLogEntity>(params).getPage(),
                new EntityWrapper<DynamicLogEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public Integer updateLeader() {

        List<DynamicLogEntity> dynamicLogList = baseMapper.selectByStatus();
        for (DynamicLogEntity dynamicLogEntity : dynamicLogList) {
            List<UserEntity> userEntityList = userService.selectIntroUserId(dynamicLogEntity.getUserId());
            BigDecimal total = BigDecimal.ZERO;
            BigDecimal count = BigDecimal.ZERO;
            for (UserEntity user : userEntityList) {
                DynamicLogEntity dynamicLog = baseMapper.selectByUser(user.getId(), dynamicLogEntity.getOrderId());
                if (dynamicLog != null) {
                    if (dynamicLogEntity.getRate().compareTo(dynamicLog.getRate()) == 1) {
                        BigDecimal subRate = MathUtil.sub(dynamicLogEntity.getRate(), dynamicLog.getRate());
                        BigDecimal rate = MathUtil.div(subRate, new BigDecimal("100"));
                        BigDecimal number = MathUtil.mul(dynamicLog.getTotal(), rate);
                        count = MathUtil.add(count, number);
                    }
                    total = MathUtil.add(total, dynamicLog.getTotal());
                }
            }
            BigDecimal rate = MathUtil.div(dynamicLogEntity.getRate(), new BigDecimal("100"));
            BigDecimal sub = MathUtil.sub(dynamicLogEntity.getTotal(), total);
            BigDecimal number = MathUtil.mul(sub, rate);
            number = MathUtil.add(number, count);
            dynamicLogEntity.setNumber(number);
            Integer result = baseMapper.updateById(dynamicLogEntity);
            if (result <= 0) {
                throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
            }
            result = baseMapper.updateStatus(dynamicLogEntity.getId());
            if (result <= 0) {
                throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
            }

            WalletOper oper = new WalletOper();
            WalletEntity walletEntity = walletService.selectByUserId(dynamicLogEntity.getUserId());
            oper.setUserId(dynamicLogEntity.getUserId());
            oper.setAmount(number);
            oper.setRemark("领导奖");
            oper.setOper(WalletOperEnum.ADD);
            if (walletEntity != null) {
                oper.setId(walletEntity.getId());
            }
            walletService.updateWallet(oper);
        }
        return 1;
    }

    @Override
    public BigDecimal selectLeaderTotal(Integer userId) {
        return baseMapper.selectLeaderTotal(userId);
    }
}
